import Vue from 'vue'
import VueRouter from 'vue-router'
import myIndex from '@/views/myIndex.vue'
import myLogin from '@/views/myLogin.vue'
import myLogined from '@/views/myLogined.vue'
import myRegister from '@/views/myRegister.vue'
import mySearch from '@/views/mySearch.vue'
import publicLogin from '@/views/publicLogin.vue'
import findPassword from '@/views/findPassword.vue'

Vue.use(VueRouter)

const routes = [
  // 重定向的路由规则
  { path: '/', redirect: '/index' },
  { path: '/index', component: myIndex },
  { path: '/login', component: myLogin },
  { path: '/logined', component: myLogined },
  { path: '/register', component: myRegister },
  { path: '/search', component: mySearch },
  { path: '/publicLogin', component: publicLogin },
  { path: '/findpassword', component: findPassword }
]

const router = new VueRouter({
  routes
})

// router.beforeEach(function (to, from, next) {
//   // to表示将要访问的路由的信息对象
//   // from表示将要离开的路由的信息对象
//   // next()函数表示放行的意思
//   if (to.path === '/logined') {
//     const token = localStorage.getItem('token')
//     // 要访问后台页面，判断是否有token
//     if (token) {
//       next()
//     } else {
//       // 没有token，强制进入登录页面
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
